
import React from 'react';
import { Router, Route, IndexRoute, browserHistory } from 'react-router';

import App from '../App.jsx';
import * as workzone from '../workzone/';
import auth from './auth.js'

/*const DynamicRoutes = (
  <Route path="/" component={App} >
    {
      auth.loggedIn() ? 
      (<IndexRoute component={workzone.BaseDataContent} />)
      :
      (<IndexRoute component={workzone.Login} />)
    }
    
    <Route path="test" component={workzone.TestContent2} />
    <Route path="fendan/single" component={workzone.SingleFendan} />
    <Route path="fendan/batch" component={workzone.BatchFendan} />
    <Route path="fendan/api" component={workzone.FendanAPI} />
    <Route path="fendan/statistics" component={workzone.FendanStatistics} />
    <Route path="fendan/addresscorrection" component={workzone.AddressCorrection} />
    <Route path="path" component={workzone.Path} />
    <Route path="basedata" component={workzone.BaseDataContent} />
  </Route>
)

export default DynamicRoutes;*/


import Login from '../login/Login.jsx';

function redirectToLogin(nextState, replace) {
  if (!auth.loggedIn()) {
    replace({
      pathname: '/login',
      state: { nextPathname: nextState.location.pathname }
    })
  }
}

function redirectToDashboard(nextState, replace) {
  if (auth.loggedIn()) {
    replace('/')
  }
}

const DynamicRoutes = {
  component: require('../App.jsx').default,
  childRoutes: [
    { 
      path: '/',
      getComponent: (nextState, cb) => {
        if (auth.loggedIn()) {
          return require.ensure([], (require) => {
            cb(null, require('../workzone/basedata/index.jsx').default)
          })
        }
        
        return require.ensure([], (require) => {
          cb(null, require('../login/Login.jsx').default)
        })
      },
      indexRoute: {
        getComponent: (nextState, cb) => {
          if (auth.loggedIn()) {
            return require.ensure([], (require) => {
              cb(null, require('../workzone/basedata/index.jsx').default)
            })
          }
          return cb()
        }
      }
    },
    { path: '/login',
      getComponent: (nextState, cb) => {
        require.ensure([], (require) => {
          cb(null, require('../login/Login.jsx'))
        })
      }
    },
    { path: '/basedata',
      getComponent: (nextState, cb) => {
        require.ensure([], (require) => {
          cb(null, require('../workzone/basedata/index.jsx').default)
        })
      }
    },
    { path: '/test',
      getComponent: (nextState, cb) => {
        require.ensure([], (require) => {
          cb(null, require('../workzone/TestContent2.jsx').default)
        })
      }
    },
    { path: '/fendan/single',
      getComponent: (nextState, cb) => {
        require.ensure([], (require) => {
          cb(null, require('../workzone/fendan/Single.jsx').default)
        })
      }
    },
    { path: '/fendan/batch',
      getComponent: (nextState, cb) => {
        require.ensure([], (require) => {
          cb(null, require('../workzone/fendan/Batch.jsx').default)
        })
      }
    },
    { path: '/fendan/api',
      getComponent: (nextState, cb) => {
        require.ensure([], (require) => {
          cb(null, require('../workzone/fendan/FendanAPI.jsx').default)
        })
      }
    },
    { path: '/fendan/statistics',
      getComponent: (nextState, cb) => {
        require.ensure([], (require) => {
          cb(null, require('../workzone/fendan/Statistics.jsx').default)
        })
      }
    },
    { path: '/fendan/addresscorrection',
      getComponent: (nextState, cb) => {
        require.ensure([], (require) => {
          cb(null, require('../workzone/fendan/AddressCorrection.jsx').default)
        })
      }
    },
    { path: '/path',
      getComponent: (nextState, cb) => {
        require.ensure([], (require) => {
          cb(null, require('../workzone/path/index.jsx').default)
        })
      }
    }
  ]
}
console.log(DynamicRoutes)
export default DynamicRoutes;